<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <script src="https://cdn.tailwindcss.com"></script> -->
    <!-- <script src="/tailwind/tailwind.css"></script> -->
    <script src="/tailwind/cdnjs/tailwind-3.4.16.js"></script>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>

    <div for="计算属性" id="app" class="container mx-auto max-w-md p-4">
        <div id="example">
            <p>Original message: "{{ message }}"</p>
            <p>Computed reversed message: "{{ reversedMessage }}"</p>
            <div class="block">
                <label for="firstname" class="block ">firstname:</label>
                <input type="text" v-model="firstname" class="border-2 border-gray-300 rounded w-full py-2 px-4">
            </div>
            <div class="block">
                <label for="lastname">lastname:</label>
                <input type="text" v-model="lastname" class="border-2 border-gray-300 rounded w-full py-2 px-4">
            </div>
            <p>fullname: "{{ fullname }}"</p>
            <p>fullname2: "{{ fullname2 }}"</p>
        </div>
    </div>

    <div for="侦听器">
        <div id="watch-example">
            <p>
                Ask a yes/no question: <br>
                <input v-model="question" class="border-2 border-gray-300 rounded w-full py-2 px-4">
            </p>
            <p>{{ answer }}</p>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
    <script>
        let watchExampleVM = new Vue({
            el: '#watch-example',
            data: {
                question: '',
                answer: 'I cannot give you an answer until you ask a question!'
            },
            watch: {
                // 监听 question 变化
                question: function (newVal, oldVal) {
                    this.answer = 'Waiting for you to stop typing...'
                    // this.debounceAnswer()
                    this.debouncedGetAnswer()
                }
            },
            created: function () {
                // `_.debounce` 是一个通过 Lodash 限制操作频率的函数。
                // 在这个例子中，我们希望限制访问 yesno.wtf/api 的频率
                // AJAX 请求直到用户输入完毕才会发出。想要了解更多关于
                // `_.debounce` 函数 (及其近亲 `_.throttle`) 的知识，
                // 请参考：https://lodash.com/docs#debounce
                this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
            },
            methods: {
                // debounceAnswer: function () {
                //     clearTimeout(this.timeout)
                //     this.timeout = setTimeout(() => {
                //         this.getAnswer()
                //     }, 500)
                // },
                // getAnswer: function () {
                //     if (this.question.endsWith('?')) {
                //         this.answer = 'Yes'
                //     } else {
                //         this.answer = 'No'
                //     }
                // }
                getAnswer: function () {
                    if (this.question.indexOf('?') === -1) {
                        this.answer = 'Questions usually contain a question mark. ;-)'
                        return
                    }
                    this.answer = 'Thinking...'
                    var vm = this
                    axios.get('https://yesno.wtf/api')
                        .then(function (response) {
                            vm.answer = _.capitalize(response.data.answer)
                        })
                        .catch(function (error) {
                            vm.answer = 'Error! Could not reach the API. ' + error
                        })
                }
            }
        })

        let vm = new Vue({
            el: '#app',
            data: {
                message: 'Hello',
                firstname: 'John',
                lastname: 'Doe',
                fullname: '',
                // fullname2: '', // 和计算属性不能同名
            },
            computed: {
                // 计算属性的 getter
                reversedMessage() {
                    // `this` 指向 vm 实例
                    return this.message.split('').reverse().join('')
                },
                fullname2() {
                    return this.firstname + '' + this.lastname;
                },
                fullname3: {
                    get() {
                        return this.firstname + '' + this.lastname;
                    },
                    set(newVal) {
                        let names = newVal.split(' ');
                        this.firstname = names[0];
                        this.lastname = names[1];
                    }
                }
            },
            watch: {
                // 监听 message 变化
                message(newVal, oldVal) {
                    console.log(`Message changed from ${oldVal} to ${newVal}`)
                },
                firstname: function (newVal) {
                    this.fullname = newVal + '' + this.lastname;
                },
                lastname: function (newVal) {
                    this.fullname = this.firstname + '' + newVal;
                }
            }
        });

    </script>
</body>

</html>